// by Xeno

#define __del \
{deleteVehicle _x} forEach [_vehicle] + _crew
private ["_player", "_sidep", "_grp", "_vehicle", "_unit", "_crew"];
if (!isServer) exitWith {};

_player = _this;
_sidep = side _player;

// TODO: I'd like to change this routine by checking the available heli on the base, spawn an AI there and use those helis.
_dstart_pos = call XfGetRanPointOuterAir;
_grp = [_sidep] call x_creategroup;
_spos = [_dstart_pos select 0, _dstart_pos select 1, 300];
_cdir = [_spos, position _player] call XfDirTo;
_veca = [_spos, _cdir, x_taxi_aircraft, _grp] call XfSpawnVehicle;
_vehicle = _veca select 0;
_crew = _veca select 1;
_unit = driver _vehicle;
d_allunits_add set [count d_allunits_add, _vehicle];

_fnTaxiGetOutHandler = {
	private ["_vec","_unit","_empty"];
	_vec = _this select 0;
	_empty = true;
	{
		If (isPlayer _x) ExitWith {_empty = false;};
	} foreach crew _vec;
	_vec setVariable ["airtaxiempty",_empty,True];
	diag_log format["TaxtGetOut: %1", _empty];
};

_vehicle setVariable ["airtaxiempty",False,True];
_vehicle addEventHandler ["getout",_fnTaxiGetOutHandler];

_vehicle lockdriver true;

[_vehicle, [position _player], 80, true] execVM "scripts\mando_heliroute_arma.sqf";

sleep 10;

if (!alive _player) exitWith {
	["d_ataxi", [1,_player]] call XNetCallEvent;
	sleep 120;
	__del;
};

["d_ataxi", [0,_player]] call XNetCallEvent;

_toldp = false;
while {_vehicle getVariable "mando_heliroute" == "busy"} do {
	if (_vehicle distance _player < 1000 && alive _player && !_toldp) then {
		["d_ataxi", [6,_player]] call XNetCallEvent;
		_toldp = true;
	};
	sleep 2.012;
};

if (_vehicle getVariable "mando_heliroute" == "damaged") exitWith {
	["d_ataxi", [2,_player]] call XNetCallEvent;
	sleep 120;
	__del;
};

if (_vehicle getVariable "mando_heliroute" == "waiting") then {
	_startWait = time;
	while {alive _player && !(_player in crew _vehicle) && ((time - _startWait) < 300)} do {sleep 1.012};
	diag_log format["Done waiting for: %1 (%2)", _player,(time - _startWait)];
	_hasplayers = false;
	{
		if (isPlayer _x) exitWith {_hasplayers = true;};
	} foreach crew _vehicle;
	If (_hasplayers) then {
		diag_log format["Players on board: %1", crew _vehicle];
		["d_ataxi", [3,_player]] call XNetCallEvent;
		if (!(alive _player) || !(_player in crew _vehicle)) then {
			diag_log format["Caller died or not on board but we'll continue: %1", crew _vehicle];
			sleep 10;
		};
		
		sleep 30 + random 5;
		["d_ataxi", [5,_player]] call XNetCallEvent;
		[_vehicle, [position AISPAWN], 80, true] execVM "scripts\mando_heliroute_arma.sqf";
		sleep 5;
		while {	_vehicle getVariable "mando_heliroute" == "busy" && !(_vehicle getVariable "airtaxiempty") } do {
			_hasplayers = false;
			{
				if (isPlayer _x) exitWith {_hasplayers = true;};
			} foreach crew _vehicle;
			if (!_hasplayers) then { _vehicle setVariable ["mando_heliroute","cancel",true]; };
			sleep 2.012
		};
		if (_vehicle getVariable "mando_heliroute" == "damaged") exitWith {
			["d_ataxi", [2,_player]] call XNetCallEvent;
			sleep 120;
			__del;
		};
		if ((_vehicle getVariable "mando_heliroute" == "waiting") || (_vehicle getVariable "airtaxiempty")) then {
			if (!(_vehicle getVariable "airtaxiempty")) then {
				while {_player in crew _vehicle} do {sleep 3.012};
				sleep 20 + random 5;
			};
			["d_ataxi", [4,_player]] call XNetCallEvent;
			
			[_vehicle, [_dstart_pos], 80, false] execVM "scripts\mando_heliroute_arma.sqf";
			_startWait = time;
			while {(_vehicle getVariable "mando_heliroute" == "busy") && ((time - _startWait) < 600)} do {sleep 2.012};
			sleep 120;
			__del;
		};
		_vehicle setVariable ["mando_heliroute","cancel",true];
		sleep 120;
		__del;
	} else {
		diag_log format["No players on board: %1", crew _vehicle];
		["d_ataxi", [1,_player]] call XNetCallEvent;
		[_vehicle, [_dstart_pos], 80, false] execVM "scripts\mando_heliroute_arma.sqf";
		sleep 240;
		__del;
	};
};